package com.inplaceedit.client.ui;

import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;

public class InPlaceEditEvent<T> extends GwtEvent<InPlaceEditEvent.Handler<T>> {

	public static interface Handler<T> extends EventHandler {
		void onInPlaceEdit(InPlaceEditEvent<T> event);
	}

	private static GwtEvent.Type<Handler<?>> TYPE;

	public static <T> InPlaceEditEvent<T> fire(
			final HasInPlaceEditHandlers<T> source, final T value) {

		final InPlaceEditEvent<T> event = new InPlaceEditEvent<T>(value);
		source.fireEvent(event);
		return event;
	}

	public static GwtEvent.Type<Handler<?>> getType() {
		if (TYPE == null) {
			TYPE = new GwtEvent.Type<Handler<?>>();
		}
		return TYPE;
	}

	private final T value;

	private InPlaceEditEvent(final T value) {
		this.value = value;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public GwtEvent.Type<Handler<T>> getAssociatedType() {
		return (GwtEvent.Type) getType();
	}

	public T getValue() {
		return value;
	}

	@Override
	protected void dispatch(final Handler<T> handler) {
		handler.onInPlaceEdit(this);
	}
}
